local Timer = _lib("timer")
local Base = _ui("base")

return Class ({
	init = function (self, x, y, label, cb, w, h)
		lume.extend(self, Base(x, y))
		local font = _lg.getFont()
		self.label = label
		self.w = w or font:getWidth(self.label) + 16
		self.h = h or font:getHeight(self.label) + 20
		self.callback = cb
		self.timers = {}
	end,
	
	is_hovered = function (self)
		local mx, my = _lm.getPosition()
		return mx >= self.x and mx <= self.x + self.w and
					 my >= self.y and my <= self.y + self.h
	end,
	
	is_clicked = function (self)
		return not self.disabled and (self:is_hovered () and _lm.isDown(1))
	end,
	
	draw = function (self)
		_lg.setColor(.3, .3, .3)
		_lg.rectangle('fill', self.x, self.y, self.w, self.h)
		_lg.setColor(1, 1, 1)
		if self.disabled then
			_lg.setColor(.4, .4, .4)
		end
		if self:is_hovered() and not self.disabled then
			_lg.rectangle('line', self.x, self.y, self.w, self.h)
		end
		_lg.print(self.label, self.x+8, self.y+8)
	end,
	
	update = function (self, dt)
		Timer.update(dt, self.timers)
		if self:is_clicked() then
			self.disabled = true
			Timer.after(.3, function () self.disabled = nil end):group(self.timers)
			if self.callback then 
				self:callback() 
			end
		end
		if self:is_hovered() then
			Game.cursor.show = nil
			Game.cursor.is_hovering = self
		end
	end,
	
})
